package com.shujia.day10;

/*
    String类中的构造方法：
        public String()
        public String(byte[] bytes)
        public String(byte[] bytes,int offset,int length)
        public String(char[] value)
        public String(char[] value,int offset,int count)
        public String(String original)
 */

public class StringDemo2 {
    public static void main(String[] args) {
        //public String() 创建一个空字符串
        String s1 = new String(); // 堆内存
        System.out.println("s1: " + s1);

//        String s2 = ""; // 常量池
//        System.out.println(s1==s2);
        System.out.println("=============================");

        //public String(byte[] bytes) 将字节数组转成一个字符串
        byte[] bytes = {97,98,99,100,101,102};
        String s2 = new String(bytes);
        System.out.println("s2: "+s2);

        System.out.println("=============================");
        //public String(byte[] bytes,int index,int length) 从字节数组的某个位置开始，向后截取变成字符串
        String s3 = new String(bytes, 2, 3);
        System.out.println("s3: "+s3);
        System.out.println("=============================");
        //public String(char[] value)  //将一个字符数组转成一个字符串
        char[] chars = {'我','爱','中','华'};
        String s4 = new String(chars);
        System.out.println("s4: "+s4);
        System.out.println("=============================");
        //public String(char[] value,int index,int length) 将字符数组一部分转成字符串
        String s5 = new String(chars,1,2);
        System.out.println("s5: "+s5);
        System.out.println("=============================");
        //public String(String original) 将字符串封装成一个String对象在堆内存中
        String s6 = new String("你好");
        System.out.println("s6: "+s6);
    }
}
